package com.nytimes.ohos.store.sample.data.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;

/**
 * Immutable implementation of {@link Images}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableImages.builder()}.
 */
@SuppressWarnings("all")
public final class ImmutableImages extends Images {
  private final Image source;

  private ImmutableImages(Image source) {
    this.source = source;
  }

  /**
   * source
   *
   * @return The value of the {@code source} attribute
   */
  @Override
  public Image source() {
    return source;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Images#source() source} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param source A new value for source
   * @return A modified copy of the {@code this} object
   */
  public final ImmutableImages withSource(Image source) {
    if (this.source == source) return this;
    Image newValue = Preconditions.checkNotNull(source, "source");
    return new ImmutableImages(newValue);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableImages} that have equal attribute values.
   *
   * @param another Object
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof ImmutableImages
        && equalTo((ImmutableImages) another);
  }

  private boolean equalTo(ImmutableImages another) {
    return source.equals(another.source);
  }

  /**
   * Computes a hash code from attributes: {@code source}.
   *
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + source.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code Images} with attribute values.
   *
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return MoreObjects.toStringHelper("Images")
        .omitNullValues()
        .add("source", source)
        .toString();
  }

  /**
   * Creates an immutable copy of a {@link Images} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   *
   * @param instance The instance to copy
   * @return A copied immutable Images instance
   */
  public static ImmutableImages copyOf(Images instance) {
    if (instance instanceof ImmutableImages) {
      return (ImmutableImages) instance;
    }
    return ImmutableImages.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableImages ImmutableImages}.
   *
   * @return A new ImmutableImages builder
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builds instances of type {@link ImmutableImages ImmutableImages}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  public static final class Builder {
    private static final long INIT_BIT_SOURCE = 0x1L;
    private long initBits = 0x1L;

    private Image source;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code Images} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     *
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(Images instance) {
      Preconditions.checkNotNull(instance, "instance");
      source(instance.source());
      return this;
    }

    /**
     * Initializes the value for the {@link Images#source() source} attribute.
     *
     * @param source The value for source 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder source(Image source) {
      this.source = Preconditions.checkNotNull(source, "source");
      initBits &= ~INIT_BIT_SOURCE;
      return this;
    }

    /**
     * Builds a new {@link ImmutableImages ImmutableImages}.
     *
     * @return An immutable instance of Images
     * @throws IllegalStateException if any required attributes are missing
     */
    public ImmutableImages build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutableImages(source);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = Lists.newArrayList();
      if ((initBits & INIT_BIT_SOURCE) != 0) attributes.add("source");
      return "Cannot build Images, some of required attributes are not set " + attributes;
    }
  }
}
